var prefix = jsctx + "/elecPrinterRecord";

var elecPrinterRecord = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageElecPrinterRecord",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [10, 20, 30],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            stationName: $("#stationName").val().trim(),
                            orderNum: $("#orderNum").val().trim(),
                            beginTime: $("#beginTime").val().trim(),
                            printerNo: $("#printerNo").val().trim(),
                            endTime: $("#endTime").val().trim(),
                            printerRtua: $("#printerRtua").val().trim(),
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'stationName', title: '站点名称', align: 'left', width: '100'},
                        {field: 'orderNum', title: '订单号', align: 'left', width: '100'},
                        {field: 'carNumber', title: '车牌号', align: 'left', width: '100'},
                        {field: 'printerNo', title: '打印机编号', align: 'left', width: '100'},
                        {field: 'printTime', title: '打印时间', width: '100', align: 'left'},
                        {field: 'printCount', title: '打印次数', width: '100', align: 'left'},
                        {field: 'printerRtua', title: '打印逻辑地址', width: '100', align: 'left'},
                        {
                            title: '操作',
                            field: '',
                            width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                var a = '';
                                if (OperationsManagement_printRecord_update) {
                                    a = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="打印机站点" onclick="elecPrinterRecord.resetPrintReceipt(\''
                                        + row.orderId
                                        + '\')">重置订单打印</a> ';
                                }
                                return a;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        window.history.back();
    }

    function resetPrintReceipt(orderId) {
        layer.confirm('是否重置该订单小票打印，重置后，该订单还能再重新打印一次小票！', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: jsctx + "/orderInfo/resetPrintReceipt",
                type: "post",
                data: {
                    'orderId': orderId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("更改成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    /*  模态框  */
    function showModel(addOrEdit) {
        console.log(addOrEdit)
        if (addOrEdit == 'edit') {

        } else if ('add') {
            $("#printerId").val('')
            $("#printerName").val('')
            $("#printerRtua").val('')
            $("#paperRestCount").val('')
            $("#warnCount").val('')
        }

        $('#exampleModal').modal('show')

        $("#addElecPrinter").unbind("click").bind("click", function () {

            var formData = new FormData();
            formData.append("printerId", $("#printerId").val());
            formData.append("printerName", $("#printerName").val());
            formData.append("printerRtua", $("#printerRtua").val());
            formData.append("paperRestCount", $("#paperRestCount").val());
            formData.append("warnCount", $("#warnCount").val());
            $.ajax({
                url: prefix + "/saveOrUpdateElecPrinter",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        //  $('#exampleModal').modal('hide')
                    }

                }
            })
        })

    }

    function exportElecPrinterRecord() {
        $.fileDownload(prefix + "/exportElecPrinterRecord", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                stationName: $("#stationName").val().trim(),
                orderNum: $("#orderNum").val().trim(),
                beginTime: $("#beginTime").val().trim(),
                printerNo: $("#printerNo").val().trim(),
                endTime: $("#endTime").val().trim(),
                printerRtua: $("#printerRtua").val().trim(),
                count: $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        showModel: showModel,
        exportElecPrinterRecord: exportElecPrinterRecord,
        resetPrintReceipt: resetPrintReceipt
    };
})();
